//
//  ReadViewController.m
//  
//
//  Created by lanou on 16/7/2.
//
//

#import "ReadViewController.h"

@interface ReadViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong)UICollectionView *collectionView;


@property (nonatomic,strong)NSMutableArray *scrollerArray;//轮播图数组
@property (nonatomic,strong)NSMutableArray *collectionarray;//数据源



@end

@implementation ReadViewController
#pragma mark---------懒加载----------

- (NSMutableArray *)scrollerArray{
    if (!_scrollerArray) {
        _scrollerArray = [NSMutableArray array];
    }
    return _scrollerArray;
}
- (NSMutableArray *)collectionarray{
    if (!_collectionarray) {
        _collectionarray = [NSMutableArray array];
    }
    return _collectionarray;
}

- (UICollectionView *)collectionView{
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        //设置item的大小
        layout.itemSize = CGSizeMake(100, 100);
        //设置间距
        layout.sectionInset = UIEdgeInsetsMake(20, 10, 20, 10);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, (kHeight-64)/3+64, kWidth, (kHeight-64)/3*2) collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
    }
    return _collectionView;
}


#pragma mark---------生命周期----------
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    //请求数据
    [RequestManager fetchDataWithURL:KReadUrl parameter:@{} metod:@"GET" compelet:^(NSData *data) {
        //请求数据，分析数据，建立模型，添加数组
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",bigDic);
        for (NSDictionary *smallDic in bigDic[@"data"][@"carousel"]) {
            NSString *a = smallDic[@"img"];
            [self.scrollerArray addObject:a];
        }
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            
            ReadCollectionModel *model = [[ReadCollectionModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.collectionarray addObject:model];

        }
        
        
        
        
        
    } updateUI:^{
       
        
         ScrollView *scroll = [[ScrollView alloc]initWithFrame:CGRectMake(0,64, kWidth ,(kHeight-64)/3) imageArray:self.scrollerArray];
        
        [self.view addSubview:scroll];
        
        [self.collectionView reloadData];
    }];
    
    
    
    
    [self.view addSubview:self.collectionView];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ReadCell" bundle:nil] forCellWithReuseIdentifier:@"readcollection_cell"];
   
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark---------collectionViewCell设置----------

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.collectionarray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    ReadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"readcollection_cell" forIndexPath:indexPath];
    [cell setCellWithModel:self.collectionarray[indexPath.row]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ReadListViewController *a = [[ReadListViewController alloc]init];
 
    ReadCollectionModel *b = self.collectionarray[indexPath.row];
    a.typeID = b.type;
    [self.navigationController pushViewController:a animated:YES];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
